home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libg++ / src / SLList.h < prev    next >
C/C++ Source or Header  |  1994-08-31  |  4KB  |  125 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /* 
  3. Copyright (C) 1988, 1992 Free Software Foundation
  4.     written by Doug Lea (dl@rocky.oswego.edu)
  5.  
  6. This file is part of the GNU C++ Library.  This library is free
  7. software; you can redistribute it and/or modify it under the terms of
  8. the GNU Library General Public License as published by the Free
  9. Software Foundation; either version 2 of the License, or (at your
  10. option) any later version.  This library is distributed in the hope
  11. that it will be useful, but WITHOUT ANY WARRANTY; without even the
  12. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  13. PURPOSE.  See the GNU Library General Public License for more details.
  14. You should have received a copy of the GNU Library General Public
  15. License along with this library; if not, write to the Free Software
  16. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #ifndef _SLList_h
  20. #ifdef __GNUG__
  21. //#pragma interface
  22. #endif
  23. #define _SLList_h 1
  24.  
  25. #include <Pix.h>
  26.  
  27. struct BaseSLNode
  28. {
  29.    BaseSLNode *tl;
  30.    void *item() {return (void*)(this+1);} // Return ((SLNode<T>*)this)->hd
  31. };
  32.  
  33. template<class T>
  34. class SLNode : public BaseSLNode
  35. {
  36.   public:
  37.     T                    hd; // Data part of node
  38.                          SLNode() { }
  39.                          SLNode(const T& h, SLNode* t = 0)
  40.                  : hd(h) { tl = t; }
  41.                          ~SLNode() { }
  42. };
  43.  
  44. extern int __SLListLength(BaseSLNode *ptr);
  45.  
  46. class BaseSLList {
  47.   protected:
  48.     BaseSLNode *last;
  49.     virtual void delete_node(BaseSLNode*node) = 0;
  50.     virtual BaseSLNode* copy_node(const void* datum) = 0;
  51.     virtual void copy_item(void *dst, void *src) = 0;
  52.     virtual ~BaseSLList() { }
  53.     BaseSLList() { last = 0; }
  54.     void copy(const BaseSLList&);
  55.     BaseSLList& operator = (const BaseSLList& a);
  56.     Pix ins_after(Pix p, const void *datum);
  57.     Pix prepend(const void *datum);
  58.     Pix append(const void *datum);
  59.     int remove_front(void *dst, int signal_error = 0);
  60.     void join(BaseSLList&);
  61.   public:
  62.     int length() const;
  63.     int empty() const { return last == 0; }
  64.     void clear();
  65.     Pix                   prepend(BaseSLNode*);
  66.     Pix                   append(BaseSLNode*);
  67.     int                   OK() const;
  68.     void                  error(const char* msg) const;
  69.     void                  del_after(Pix p);
  70.     int                   owns(Pix p) const;
  71.     void                  del_front();
  72. };
  73.  
  74. template <class T>
  75. class SLList : public BaseSLList
  76. {
  77.   private:
  78.     virtual void delete_node(BaseSLNode *node) { delete (SLNode<T>*)node; }
  79.     virtual BaseSLNode* copy_node(const void *datum)
  80.     { return new SLNode<T>(*(const T*)datum); }
  81.     virtual void copy_item(void *dst, void *src) { *(T*)dst = *(T*)src; }
  82.  
  83. public:
  84.     SLList() : BaseSLList() { }
  85.     SLList(const SLList<T>& a) : BaseSLList() { copy(a); }
  86.     SLList<T>&            operator = (const SLList<T>& a)
  87.     { BaseSLList::operator=((const BaseSLList&) a); return *this; }
  88.     virtual ~SLList() { clear(); }
  89.  
  90.     Pix prepend(const T& item) {return BaseSLList::prepend(&item);}
  91.     Pix append(const T& item) {return BaseSLList::append(&item);}
  92.     Pix prepend(SLNode<T>* node) {return BaseSLList::prepend(node);}
  93.     Pix append(SLNode<T>* node) {return BaseSLList::append(node);}
  94.  
  95.     T& operator () (Pix p) {
  96.     if (p == 0) error("null Pix");
  97.     return ((SLNode<T>*)(p))->hd; }
  98.     const T& operator () (Pix p) const {
  99.     if (p == 0) error("null Pix");
  100.     return ((SLNode<T>*)(p))->hd; }
  101.     inline Pix first() const { return (last == 0) ? 0 : Pix(last->tl); }
  102.     void next(Pix& p) const
  103.     { p = (p == 0 || p == last) ? 0 : Pix(((SLNode<T>*)(p))->tl); }
  104.     Pix ins_after(Pix p, const T& item)
  105.       { return BaseSLList::ins_after(p, &item); }
  106.     void join(SLList<T>& a) { BaseSLList::join(a); }
  107.     
  108.     T& front() {
  109.     if (last == 0) error("front: empty list");
  110.     return ((SLNode<T>*)last->tl)->hd; }
  111.     T& rear() {
  112.     if (last == 0) error("rear: empty list");
  113.     return ((SLNode<T>*)last)->hd; }
  114.     const T& front() const {
  115.     if (last == 0) error("front: empty list");
  116.     return ((SLNode<T>*)last->tl)->hd; }
  117.     const T& rear() const {
  118.     if (last == 0) error("rear: empty list");
  119.     return ((SLNode<T>*)last)->hd; }
  120.     int remove_front(T& x) { return BaseSLList::remove_front(&x); }
  121.     T remove_front() { T dst; BaseSLList::remove_front(&dst, 1); return dst; }
  122. };
  123.  
  124. #endif
  125.